/**
 * 函数防抖 （只执最后一次点击）多用于频繁点击和搜索框输入补全
 * @param fn
 * @param delay
 * @returns {function}
 */
export const _debounce = (fn, t) => {
    let delay = t || 500;
    let timer;
    // console.log(typedf fn)
    return function() {
        let args = arguments;
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            timer = null;
            fn.apply(this, args);
        }, delay);

    }

};

/**
 * 函数节流 多用于上拉加载事件
 * @param fn
 * @param interval
 * @returns {function}
 */

export const _throttle = (fn, t) => {
    let last;
    let timer;
    let interval = t || 500;

    return function() {
        let args = arguments;
        let now = +new Date();

        if (last && now - last < interval) {
            clearTimeout(timer);
            timer = setTimout(() => {
                last = now;
                fn.apply(this, args);

            }, interval);
        } else {
            last = now;
            fn.apply(this, args);
        }

    }
};


/**
 * 排序 选取最大值
 * @param fn
 * @param interval
 * @returns {function}
 */

var num = [22, 45, 14, 78, 57, 30, 89, 43, 25];



// export const _Sort = ()

//});
//获取商品接口 上拉加载
// goods: _throttle(function() {
//     var that = this;
//     var car = that.car;
//     var userinfo = that.$store.state.userinfo;
//     that.page = that.page + 1;
//     that.$axios.get("/getcarlist", {
//             params: {
//                 limit: 6,
//                 page: that.page,
//                 user_id: 48986377 //userinfo.user_id
//             },
//             headers: {
//                 "token": '', //userinfo.api_token
//             }
//         })
//         .then(res => {
//             that.$toast.clear();
//             that.car_succ(res.data, car, that.page);
//         }).catch(function(error) {
//             console.log("error", error)
//         })
// }, 200),


//rule校验开发

//账号校验
export const validateCode = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('请输入账号'))
    }
    if (!/^(?![0-9]*$)(?![a-zA-Z]*$)[a-zA-Z0-9]{6,20}$/.test(value)) {
        callback(new Error('账号必须为6-20位字母和数字组合'))
    } else {
        callback()
    }
}

//密码校验


export const validatePsdReg = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('请输入密码'))
    }
    if (!/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/.test(value)) {
        callback(new Error('请输入6-20位英文字母、数字或者符号（除空格），且字母、数字和标点符号至少包含两种'))
    } else {
        callback()
    }
}